﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CustomControls_LostPant : System.Web.UI.UserControl
{
    protected int Id
    {
        get
        {
            if (ViewState["id"] == null) return 0;
            return (int)ViewState["id"];
        }
        set { ViewState["id"] = value; }
    }
    protected int ProductionId
    {
        get
        {
            if (ViewState["productionId"] == null) return 0;
            return (int)ViewState["productionId"];
        }
        set { ViewState["productionId"] = value; }
    }
    protected String[] Message
    {
        get
        {
            if (Session["Message"] == null)
            {
                String[] m = {"",""};
                return m;
            }
            return (String[])Session["Message"];
        }
        set { Session["Message"] = value; }
    }
    private bool Add
    {
        get
        {
            if (ViewState["add"] != null)
            {
                return (bool)ViewState["add"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["add"] = value;
        }
    }
    private bool Edit
    {
        get
        {
            if (ViewState["Edit"] != null)
            {
                return (bool)ViewState["Edit"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["Edit"] = value;
        }
    }
    private bool View
    {
        get
        {
            if (ViewState["View"] != null)
            {
                return (bool)ViewState["View"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["View"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            String[] msg = { "", "" };
            Message = msg;
            List<BLLLaundry.sizesByProduction> sizes = new List<BLLLaundry.sizesByProduction>();
            BLLLaundry bll = new BLLLaundry();
            if ( !String.IsNullOrEmpty(Request.QueryString["prodId"]))
            {
                int pid = 0;
                int.TryParse(Request.QueryString["prodId"], out pid);
                this.ProductionId = pid;
            }
            LoadSizes();
            if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Request.Url.ToString().Contains("Editar.aspx")
                && !String.IsNullOrEmpty(Request.QueryString["prodId"]))
            {
                int cid = 0;
                int.TryParse(Request.QueryString["id"], out cid);
                this.Id = cid;
                BLLLaundry.Full_LostPants p = bll.getFullLostPantById(this.Id);
               if (p.Id > 0)
                {
                    populateInfo(p);
                }
                else
                {
                    String[] error = { Resources.Resource.ERROR, Resources.Resource.LOSTPANTS_NOT_FOUND };
                    Message = error;
                    this.Response.Redirect("Default.aspx?recover=y");
                }
            }
            else
            {
                if ((Request.QueryString["id"] == null || Request.QueryString["prodId"] == null || !String.IsNullOrEmpty(Request.QueryString["id"]) ) && Request.Url.ToString().Contains("Editar.aspx"))
                {
                    String[] error = { Resources.Resource.ERROR, Resources.Resource.INVALID_URL };
                    Message = error;
                    this.Response.Redirect("Default.aspx?recover=y");
                }
                else 
                {
                    sizes = bll.getSizes();
                }
            }

           SetupPageByUserRoles();

        }
        ((Literal)Page.Master.FindControl("ltrShowMessage")).Text = "0";
    }
    private void LoadSizes()
    {
        BLLLaundry bll = new BLLLaundry();
        List<Size> list = bll.GetSizesByProductionId(this.ProductionId);
        drpSize.Items.Clear();
        drpSize.Items.Add(new ListItem("Seleccione...", ""));
        foreach (Size s in list)
        {
            drpSize.Items.Add(new ListItem(s.Name, s.Id.ToString()));
        }
    }

    private void populateInfo(BLLLaundry.Full_LostPants p)
    {
        drpSize.SelectedValue = p.SizeId.ToString();
        txtDate.Text = p.Date.ToString("dd/MM/yyyy");
        txtQuantity.Text = p.Quantity.ToString();
 
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        
        LostPant lp = new LostPant();
        long sizeId = 0;
        long.TryParse(drpSize.SelectedValue, out sizeId);
        lp.SizeId = sizeId;
        DateTime date = new DateTime();
        date = DateTime.ParseExact(txtDate.Text, "dd/MM/yyyy", null);
        lp.Date = date;
        lp.ProductionId = this.ProductionId;
        lp.Quantity = int.Parse(txtQuantity.Text);
        if (this.Id == 0)
        {
            lp.CreatedBy = Util.GetCurrentUserId();
            lp.DateCreated = DateTime.Now;
        }
        else
        {
            lp.Id = this.Id;
        }
        BLLLaundry bll = new BLLLaundry();
        if (Convert.ToInt32(txtQuantity.Text) <= bll.GetPantsByProductionId(this.ProductionId))
        {
            if (bll.SaveLostPants(lp))
            {
                String[] error = { Resources.Resource.SUCCESS, Resources.Resource.LOST_PANTS_CREATED };
                Message = error;
                this.Response.Redirect("Default.aspx?id=" + this.ProductionId + "&recover=y");
            }
            else
            {
                ((Literal)Page.Master.FindControl("ltrShowMessage")).Text = "1";
                ((Literal)Page.Master.FindControl("ltrMessage")).Text = Resources.Resource.UPDATE_ERROR;
                ((Literal)Page.Master.FindControl("ltrMessageType")).Text = Resources.Resource.ERROR;
            }
        }
        else
        {
            this.lblQuantityError.Visible = true;
            
        }
        
        
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("Default.aspx?id=" + this.ProductionId + "&recover=y");
    }
    protected void SetupPageByUserRoles()
    {
        List<SessionPermissions> userPermissions = new List<SessionPermissions>();
        userPermissions = (List<SessionPermissions>)Session[GlobalNames.UserPermissions];
        userPermissions = (from upb in userPermissions
                           where upb.ModuleId == (long)GlobalEnums.Modules.PantalonesExtraviados
                           select upb).ToList();
        if (userPermissions.Count() > 0)
        {
            foreach (SessionPermissions up in userPermissions)
            {
                if (up.PermissionId == (long)GlobalEnums.Permissions.Agregar)
                    Add = true;
                if (up.PermissionId == (long)GlobalEnums.Permissions.Editar)
                    Edit = true;
                if (up.PermissionId == (long)GlobalEnums.Permissions.Ver)
                    this.View = true;
            }
            if (!View)
                this.Response.Redirect("~/Default.aspx");
            if (!Add && Id == 0)
            {
                this.Response.Redirect("Default.aspx?id="+this.ProductionId);
            }
            if (Id > 0 && !Edit)
                btnSave.Visible = false;
        }
        else
        {
            this.Response.Redirect("~/Default.aspx");
        }
    }
    
    
}